package com.wlz;


import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *  定时线程池
 * @author wlz
 * @date 2022-04-19  10:09 下午
 */
public class ScheduleThreadPoolRunner {

    public static void main(String[] args) throws Exception {


        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
        System.out.println("main start .......");

//        test1(scheduledThreadPoolExecutor);
//        test2(scheduledThreadPoolExecutor);
//        test3(scheduledThreadPoolExecutor);
        test4();

        System.out.println("main end ....");

    }

    /**
     * Timer  周期性 单线程的
     */
    public static void test4() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println("fadsfasdfa");
//                throw new RuntimeException("unexpected error, stop working ...");
            }
        },1000, 2000);

//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        timer.scheduleAtFixedRate(new TimerTask() {
//            @Override
//            public void run() {
//                System.out.println("fadsfasdfa23232");
//                throw new RuntimeException("unexpected error, stop working 312423 ...");
//            }
//        },1000, 2000);
    }

    /**
     *  scheduleAtFixedRate 周期性， 多线程的 不会覆盖(超时之后也会推迟2s，再执行), 需要异常捕获，不然任务会丢
     * @param scheduledThreadPoolExecutor
     */
    public static void test3(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) {
        scheduledThreadPoolExecutor.scheduleWithFixedDelay(() -> {
            System.out.println("send heart beat ...");
            long startTime = System.currentTimeMillis(), nowTime = startTime;
            while ((nowTime - startTime) < 5000) {
                nowTime = System.currentTimeMillis();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("task over ....");
//            throw new RuntimeException("unexpected error, stop working ...");
        }, 1000, 2000, TimeUnit.MILLISECONDS); // 延后一秒钟， 每两秒钟执行一次,不会覆盖
    }

    /**
     *   scheduleAtFixedRate 周期性，多线程的  会覆盖(超时之后不会推迟2s，直接执行) 需要异常捕获，不然任务会丢
     * @param scheduledThreadPoolExecutor
     */
    public static void test2(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) {
        scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {
            System.out.println("send heart beat ...");
            long startTime = System.currentTimeMillis(), nowTime = startTime;
            while ((nowTime - startTime) < 5000) {
                nowTime = System.currentTimeMillis();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("task over ....");
            throw new RuntimeException("unexpected error, stop working ...");
        }, 1000, 2000, TimeUnit.MILLISECONDS); // 延后一秒钟， 每两秒钟执行一次， 会覆盖
    }

    /**
     *  只能执行 一次里面的任务
     * @param scheduledThreadPoolExecutor
     */
    public static void test1(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) {

        //        // 延迟5s 执行， 不会阻塞 主线程
//        scheduledThreadPoolExecutor.schedule(() -> {
//            System.out.println("延迟5s执行 ....");
//        }, 5000, TimeUnit.MILLISECONDS);

        // 延迟5s 执行 带返回值，
        ScheduledFuture<Integer> future = scheduledThreadPoolExecutor.schedule(() -> {
            System.out.println("延迟5s执行 ....");
            return 1;
        }, 5000, TimeUnit.MILLISECONDS);

//        System.out.println(future.get()); // 会阻塞主线程
    }
}
